package view.frontend.order;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.border.TitledBorder;
import model.CustomerSpecified;
import view.ViewHandler;
import view.ViewSingleton;
import view.generaldesign.DesignLayout;
import view.menu.MenuLabel;

/**
 *
 * @author Sonny Sandberg
 */
public class OrderCustomerList 
{
    private JPanel 
            orderCustomerListPanel,
            existingCustomersPanel,
            scrollPanel;
    private Order order;
    private JScrollPane scrollPane;

    public OrderCustomerList(Order view)
    {
        order = view;
        
        orderCustomerListPanel = new JPanel(null);
        orderCustomerListPanel.setBounds(0, 90, 300, 370);
        orderCustomerListPanel.setOpaque(false);
        
        existingCustomersPanel = new JPanel(null);
        
        scrollPane = new JScrollPane();
        buildExistingCustomersPanel();
        
        orderCustomerListPanel.setVisible(false);
    }
    
    private void buildExistingCustomersPanel()
    {
        existingCustomersPanel.setBorder(BorderFactory.createTitledBorder(DesignLayout.T_FIELD_RED, "Vælg Venligst", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, DesignLayout.STANDARD));
        existingCustomersPanel.setBounds(0, 90, 300, 370);
        existingCustomersPanel.setOpaque(false);
        
        scrollPanel = new JPanel(null);
        scrollPanel.setBackground(DesignLayout.T_FIELD_BG_COLOR);
        scrollPanel.setBackground(Color.white);
        scrollPanel.setBorder(null);
        
        scrollPane.setBorder(DesignLayout.T_FIELD_RED);
        scrollPane.setBounds(20, 34, 260, 320);
        scrollPane.setViewportView(scrollPanel);
        scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        scrollPane.getVerticalScrollBar().setUnitIncrement(15);
        
        existingCustomersPanel.add(scrollPane);
        existingCustomersPanel.setVisible(false);
        orderCustomerListPanel.add(existingCustomersPanel);
    }
    
    public void buildFoundPanel(CustomerSpecified[] customerSpecified)
    {
        scrollPanel.removeAll();
        scrollPane.getViewport().setViewPosition(new Point(0,0));
        
        for (int i = 0; i < customerSpecified.length; i++)
        {
            OrderCustomerListPanel panel = new OrderCustomerListPanel(order, customerSpecified[i]);
            panel.setBounds(10, 10 + (i * 140), 220, 130);
            scrollPanel.setPreferredSize(new Dimension(220, 150 + (i * 140)));
            scrollPanel.add(panel);
        }
        
        if (customerSpecified.length == 0)
        {
            JPanel panel = new JPanel(null);
            panel.setOpaque(false);
            panel.setBounds(10, 10, 220, 130);
            scrollPanel.add(panel);
            
            JLabel label = new JLabel("<html><div style='text-align:center; width: 170px;'><b>Ingen Resultater</b><br /><i>Klik Her For At Oprette Kunde</i></div></html>");
            label.setBounds(0, 0, 220, 130);
            label.setOpaque(true);
            label.setBorder(DesignLayout.T_FIELD_RED);
            label.setBackground(DesignLayout.T_FIELD_BG_COLOR);
            label.addMouseListener(new MouseAdapter()
            {
                @Override
                public void mouseReleased(MouseEvent e)
                {
                    ViewHandler.toggleAllsView("orderContainer", false);
                    ViewHandler.toggleAllsView("menuLeft", true);
                    ViewHandler.setPageTitle("Administrér Kunde");
                    ViewHandler.toggleAllsView("adminCustomer", true);
                    ViewSingleton.getSingletonObject().getAdminCustomer().getAdminTelephone().setNumber(ViewSingleton.getSingletonObject().getOrderCustomer().gettFieldNumber());
                    ViewSingleton.getSingletonObject().getAdminCustomer().getTelephoneModel().setNumber(ViewSingleton.getSingletonObject().getOrderCustomer().gettFieldNumber());
                    ViewSingleton.getSingletonObject().getAdminCustomer().updateNewCustomerPanel();                    
                    
                    // Reset top menu background colors
                    view.ViewSingleton.getSingletonObject().getMenus()[0].resetBackgroundColor(DesignLayout.T_FIELD_BORDER_RED);
                    
                    // Now change the ADMIN menu to selected color
                    ArrayList<MenuLabel> menuLabels = ViewSingleton.getSingletonObject().getMenuLabels();
                    for (MenuLabel value : menuLabels)
                    {
                        if (value.getMenuLabelText().equals("ADMIN"))
                        {
                            value.setBackgroundColor();
                        }
                    }
                }
            });
            
            panel.add(label);
        }
        
        scrollPanel.revalidate();
        scrollPanel.repaint();       
    }

    public JPanel getPanel()
    {
        return existingCustomersPanel;
    }
}